package models

import (
	"course-srv/basic/config"
	"gorm.io/gorm"
)

// 用户
type User struct {
	gorm.Model
	Mobile   string `gorm:"type:char(11);unique;comment:'手机号'"`
	Password string `gorm:"type:varchar(32);comment:'密码'"`
	Status   int    `gorm:"type:int;default:1;comment:'状态 1-普通用户 2-vip用户'"`
}

func (u *User) TableName() string {
	return "user"
}

// 注册
func (u *User) Register() error {
	return config.DB.Create(&u).Error
}

// 根据id查询
func (u *User) GetUserById(id int) error {
	return config.DB.Model(&User{}).Where("id = ?", id).Limit(1).Find(&u).Error
}

// 根据手机号查询
func (u *User) GetUserInfo(mobile string) error {
	return config.DB.Model(&User{}).Where("mobile = ?", mobile).Limit(1).Find(&u).Error
}
